package com.zues.samples.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import com.zues.samples.dao.BaseHibernateDAO;
import com.zues.samples.dao.SubjectDAO;
import com.zues.samples.model.PagerModel;
import com.zues.samples.model.Subject;
import com.zues.samples.service.modelVO.SubjectVO;
import com.zues.samples.util.SystemContext;

@Repository
public class SubjectDAOImpl extends BaseHibernateDAO<Subject> implements SubjectDAO {

	/**
	 * 查找全部科目
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Subject> findAllSubjects() throws Exception {
		String hql = "FROM Subject";
		List<Subject> subjectlist = this.createQuery(hql).list();
		return subjectlist;
	}

	/**
	 * 通过Id 查找科目
	 */
	@Override
	public Subject getByIdSubject(int SubId) throws Exception {
		Subject subject = new Subject();
		subject = this.get(SubId);
		return subject;
	}

	/**
	 * 创建一个科目
	 */
	@Override
	public void createSubject(Subject sub) throws Exception {
		this.save(sub);
	}

	/**
	 * 编辑一个科目内容
	 */
	@Override
	public void editSubject(Subject sub) throws Exception {
		Subject exsitsSubject = this.get(sub.getSubId());
		if (exsitsSubject != null) {
			this.update(sub);
		}
	}

	/**
	 * 删除一个科目
	 */
	@Override
	public void deleteSubject(int subId) throws Exception {
		Subject exsitsSubject = this.get(subId);
		if (exsitsSubject != null) {
			this.delete(exsitsSubject);
		}
	}

	/**
	 * 分页找科目
	 */
	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<SubjectVO> findAllByPage() {
		PagerModel<SubjectVO> page = new PagerModel<SubjectVO>();
		ArrayList<SubjectVO> dates = new ArrayList<SubjectVO>();

		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getSize();

		List<Subject> tempDates = this.createQuery("FROM Subject").setFirstResult(offset).setMaxResults(pageSize)
				.list();
		for (Subject subject : tempDates) {
			SubjectVO subjectVO = new SubjectVO();
			BeanUtils.copyProperties(subject, subjectVO);
			dates.add(subjectVO);
		}
		page.setDates(dates);
		page.setOffset(offset);
		page.setPageSize(pageSize);

		// 获取总记录数
		long totalSize = (long) this.createQuery("select count(*) from Subject").uniqueResult();
		page.setTotalSize(totalSize);

		return page;
	}

	@Override
	public void updateStatus(Integer SubId, Integer status) throws Exception {
		StringBuilder sql = new StringBuilder();
		sql.append(" update exam_subject set ");
		sql.append(" status = :status ");
		sql.append(" where sub_id =  :SubId ");
		
		this.createSqlQuery(String.valueOf(sql)).setInteger("status", status)
		.setInteger("SubId", SubId).executeUpdate();
		
//		String hql = "update Subject sub set sub.status = :status where sub.subId = :SubId";

//		Query query = this.createQuery(hql);
//		query.setInteger("status", SubId);
//		query.setInteger("SubId", status);
//		query.executeUpdate();
	}

}
